// 生成树
function generateMenuTree(){
    $.ajax({
        url:"menu/get/whole/tree.json",
        type:"POST",
        dataType:"json",
        success:function (response){
            var result = response.result;
            if (result == "SUCCESS"){
                // 1 创建json对象用于存储对zTree所做的设置
                var setting = {
                    "view":{
                        "addDiyDom":myAddDiyDom,
                        "addHoverDom":myAddHoverDom,
                        "removeHoverDom":myRemoveHoverDom
                    },
                    "data":{
                        "key":{
                            "url":"false"
                        }
                    }
                };

                // 2 准备生成树形结构的json数据
                var zNodes = response.data;
                // 3 初始化树形结构
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            }
            if (result == "FALIED"){
                layer.msg(response.message)
            }
        },
        error:function (response){
            layer.msg(response.message)
        }
    })
}


// 显示图标myAddDiyDom
// treeId 生成动态数据的静态代码的id
function myAddDiyDom(treeId, treeNode) {

    // treeNode 当前树形节点的全部数据
    // console.log(treeNode);

    // zTree生成id的规则
    // eg.treeDemo_7_ico
    // 解析:ul标签的id_当前节点的序号_功能
    // 提示：ul标签的id_当前节点的序号-->可通过访问treeNode的tId属性得到
    var spanId = treeNode.tId + "_ico";
    // 删除旧的class,添加新class
    $("#" + spanId).removeClass().addClass(treeNode.icon);
}

// 鼠标移入节点添加按钮
function myAddHoverDom(treeId, treeNode) {
    // 为了给追加的标签加上Id
    var btnGroupId = treeNode.tId + "_btnGrp";

    // 判断是否为第一次添加
    if ($("#" + btnGroupId).length > 0) {
        return;
    }

    var addBtn = "<a id='"+treeNode.id+"' class=\"btn btn-info dropdown-toggle btn-xs addBtn\" style=\"margin-left:10px;padding-top:0px;\" href=\"#\"title=\"添加节点\">&nbsp;&nbsp;<i class=\"fa fa-fw fa-plus rbg \"></i></a>";
    var removeBtn = "<a id='"+treeNode.id+"'  class=\"btn btn-info dropdown-toggle btn-xs removeBtn\" style=\"margin-left:10px;padding-top:0px;\" href=\"#\"title=\"删除节点\">&nbsp;&nbsp;<i class=\"fa fa-fw fa-times rbg \"></i></a>";
    var editBtn = "<a  id='"+treeNode.id+"' class=\"btn btn-info dropdown-toggle btn-xs editBtn\" style=\"margin-left:10px;padding-top:0px;\" href=\"#\" title=\"修改节点\">&nbsp;&nbsp;<i class=\"fa fa-fw fa-edit rbg \"></i></a>";
    var editAuthBtn = "<a  id='"+treeNode.id+"' class=\"btn btn-info dropdown-toggle btn-xs editAuthBtn\" style=\"margin-left:10px;padding-top:0px;\" href=\"#\" title=\"修改权限\">&nbsp;&nbsp;<i class=\"glyphicon glyphicon-check \"></i></a>";

    // 获取当前节点的级别数据
    var level = treeNode.level;

    // 声明一个变量存储拼装好的代码
    var btnHTML = "";

    // 判断当前节点的级别
    if (level == 0){
        btnHTML = addBtn;
    }
    if (level == 1){
        btnHTML = addBtn + " " + editBtn;
        if (treeNode.children.length == 0){
            btnHTML = btnHTML + removeBtn;
        }
    }
    if (level == 2){
        btnHTML =  editAuthBtn + " " + removeBtn + " " + editBtn;
    }

    // 按钮组出现的位置在节点treeDemo_n_a超链接后面
    $("#" + treeNode.tId + "_a").after("<span id='" + btnGroupId + "'>"+btnHTML+"</span>")
}

// 鼠标移出节点删除按钮
function myRemoveHoverDom(treeId, treeNode) {
    var btnGroupId = treeNode.tId + "_btnGrp";
    $("#" + btnGroupId).remove();
}